package com.pactera.asmp.server.restfullapi;

import com.pactera.asmp.server.common.constants.ResultCode;
import com.pactera.asmp.server.common.exception.CustomException;
import com.pactera.asmp.server.entity.ResponseResult;
import com.pactera.asmp.server.pojo.FileParse;
import com.pactera.asmp.server.service.IFileParseService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import lombok.extern.slf4j.Slf4j;

/**
 * @ProjectName: tlsserver
 * @Package com.pactera.asmp.server.restfullapi
 * @ClassName: RestfullFileUploadApi
 * @Description: 文件上传解析
 * @Date 2020/06/01 11:05
 * @Author lvzhiqiang
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/v1/dbc")
@Slf4j
public class RestfullFileUploadApi {
    @Autowired
    private IFileParseService fileParseService;

    /**
     * @api {POST} /api/v1/dbc/upload uploadFile
     * @apiVersion 1.0.0
     * @apiGroup RestfullFileUploadApi
     * @apiName uploadFile
     * @apiDescription 上传dbc文件并解析
     * @apiParam (请求参数) {Object} file 文件
     * @apiParam (请求参数) {Number} projectId 项目ID
     * @apiParamExample 请求参数示例
     * file=null&projectId=9282
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccessExample 响应结果示例
     * {"code":1834,"data":{},"message":"ySwwubKEW"}
     */
    @PostMapping("/upload")
    public ResponseResult uploadFile(@RequestParam("file") MultipartFile file, Integer projectId) {
        ResponseResult responseResult = null;
        if(null == file || null == projectId || 0 == projectId){
            responseResult = new ResponseResult(ResultCode.PARAM_IS_BLANK);
            return responseResult;
        }
        FileParse fileParse = fileParseService.uploadAndInsert(file, projectId);
        // 异步解析文件上传并更新数据库
        try {
            fileParseService.parseFileAndUpdate(file.getInputStream(), fileParse.getCid());
        } catch (Exception e) {
            log.error("/api/v1/dbc/upload parseFileAndUpdate error: ", e);
            throw new CustomException(ResultCode.FILE_PARSE_FAIL);
        }
        responseResult = new ResponseResult(ResultCode.SUCCESS);
        responseResult.setData(fileParse);
        return responseResult;
    }

    /**
     * @api {GET} /api/v1/dbc/listByProjectId listByProjectId
     * @apiVersion 1.0.0
     * @apiGroup RestfullFileUploadApi
     * @apiName listByProjectId
     * @apiDescription 根据项目ID获取dbc文件接口
     * @apiParam (请求参数) {Number} projectId 项目ID
     * @apiParamExample 请求参数示例
     * projectId=4025
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccessExample 响应结果示例
     * {"code":2349,"data":{},"message":"0RQFB5c"}
     */
    @GetMapping("/listByProjectId")
    public ResponseResult listByProjectId(Integer projectId) {
        log.debug("listByProjectId projectId: {}", projectId);
        ResponseResult responseResult;
        if (null == projectId || 0 == projectId) {
            responseResult = new ResponseResult(ResultCode.PARAM_IS_BLANK);
            return responseResult;
        }
        responseResult = new ResponseResult(ResultCode.SUCCESS);
        responseResult.setData(fileParseService.selectByProjectId(projectId));
        return responseResult;
    }

    /**
     * @api {GET} /api/v1/dbc/findOne findOne
     * @apiVersion 1.0.0
     * @apiGroup RestfullFileUploadApi
     * @apiName findOne
     * @apiDescription 根据dbc文件表主键获取
     * @apiParam (请求参数) {Number} cid dbc文件表主键
     * @apiParamExample 请求参数示例
     * cid=2418
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccessExample 响应结果示例
     * {"code":7769,"data":{},"message":"maBTI"}
     */
    @GetMapping("/findOne")
    public ResponseResult findOne(Long cid) {
        log.debug("findOne cid: {}", cid);
        ResponseResult responseResult;
        if (null == cid || 0 == cid) {
            responseResult = new ResponseResult(ResultCode.PARAM_IS_BLANK);
            return responseResult;
        }
        responseResult = new ResponseResult(ResultCode.SUCCESS);
        responseResult.setData(fileParseService.findByPrimaryKey(cid));
        return responseResult;
    }

    /**
     * @api {GET} /api/v1/dbc/getDbc getDbc
     * @apiVersion 1.0.0
     * @apiGroup RestfullFileUploadApi
     * @apiName getDbc
     * @apiDescription 根据主键获取 dbc 解析后的文件
     * @apiParam (请求参数) {Number} dbcId dbc主键
     * @apiParamExample 请求参数示例
     * dbcId=6748
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccessExample 响应结果示例
     * {"code":6971,"data":{},"message":"HVqiMu"}
     */
    @GetMapping("/getDbc")
    public ResponseResult getDbc(Long dbcId) {
        log.debug("getDbc dbcId: {}", dbcId);
        ResponseResult responseResult;
        if (null == dbcId || 0 == dbcId) {
            responseResult = new ResponseResult(ResultCode.PARAM_IS_BLANK);
            return responseResult;
        }
        responseResult = new ResponseResult(ResultCode.SUCCESS);
        responseResult.setData(fileParseService.getDbcParse(dbcId));
        return responseResult;
    }
}
